package com.game.server.db.dao;

import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubJijinLog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.sql.Types;

public class XyClubJijinLogDao {

    public static void insertInto(XyClubJijinLog info) {
        String sql = "INSERT INTO xy_club_jijin_log (cid,pid,uid,integral,residual_funds,type,createdate) VALUES (?,?,?,?,?,?,NOW())";
        Parameter p1 = new Parameter(Types.INTEGER, info.getCid(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, info.getPid(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, info.getUid(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, info.getIntegral(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, info.getResidual_funds(), ParameterType.Input);
        Parameter p6 = new Parameter(Types.INTEGER, info.getType(), ParameterType.Input);
        SqlHelper.execute(sql, p1, p2, p3, p4, p5, p6);
    }

    /**
     * 红利操作明细
     *
     * @param limit  查询条数
     * @param page   页数
     * @param clubid 俱乐部ID
     * @param type    操作类型 1 充值基金 2发放基金
     * @return
     */
    public static JSONObject findDetailPage(int limit, Integer page, int clubid, int type) {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        String where = "";
        if (type != 0) {
            where = " and type = " + type + " ";
        }
        String countSql = "select count(id) from xy_club_jijin_log where cid=" + clubid + " " + where;
        String sumSql = "select IFNULL(sum(integral),0) from xy_club_jijin_log where cid=" + clubid + " " + where;
        String sql = "select pid,uid,integral,residual_funds,type,createdate from xy_club_jijin_log where cid=" + clubid + " "+where+" ORDER BY createdate desc LIMIT ?," + limit;
        ;
        Object scalar = SqlHelper.execScalar(countSql);
        int sumCount = Integer.parseInt(scalar.toString());
        int sumPage = (int) Math.ceil((double) sumCount / limit);
        page = page < 1 ? 1 : page > sumPage ? sumPage : page;
        page = page < 1 ? 1 : page;
        Parameter p1 = new Parameter(Types.INTEGER, ((page - 1) * limit), ParameterType.Input);
        CachedRowSet resultSet = SqlHelper.getResultSet(sql, p1);
        jsonObject.put("page", page);
        jsonObject.put("allpage", sumPage);
        jsonObject.put("allcount", sumCount);
        jsonObject.put("allFoundation", SqlHelper.execScalar(sumSql).toString());
        try {
            while (resultSet.next()) {
                JSONObject jsonData = new JSONObject();
                jsonData.put("pid", resultSet.getInt(1));
                jsonData.put("uid", resultSet.getInt(2));
                jsonData.put("integral", resultSet.getInt(3));
                jsonData.put("residualFunds", resultSet.getInt(4));
                jsonData.put("type", resultSet.getInt(5));
                jsonData.put("createdate", resultSet.getTimestamp(6).getTime());
                array.add(jsonData);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        jsonObject.put("array", array);
        return jsonObject;
    }
}
